Shader "Phalanx/Matrix/WorldPositionToScreenPosExample3"
{
    SubShader
    { 
        Pass
        {
            Cull Back
            
            CGPROGRAM
            #pragma shader_feature _ _USE_DEBUG_MVP_MATRIX

            #pragma vertex vert
            #pragma fragment frag
            
            #include "UnityCG.cginc"
            
            struct v2f
            {
                float4 positionCS : SV_POSITION;
                float2 uv : TEXCOORD0;
                float2 positionScreen01 : TEXCOORD1;
            };
            
            /*
            Shader 中的内置矩阵:
            
            UNITY_MATRIX_M     <=> Object To World Matrix <=> C# Unity 本地坐标系 Render.transform.localToWorldMatrix
            UNITY_MATRIX_I_M   <=> World To Object Matrix <=> C# Unity 本地坐标系 Render.transform.worldToLocalMatrix
            UNITY_MATRIX_V     <=> World To View Matrix   <=> worldToCameraMatrix
            UNITY_MATRIX_P     <=> View To HClip Matrix   <=> GL.GetGPUProjectionMatrix(camera.projectionMatrix, true);
            
            UNITY_MATRIX_MV    <=> Object To View Matrix  <=> worldToCameraMatrix * localToWorldMatrix
            UNITY_MATRIX_VP    <=> World To HClip Matrix  <=> GL.GetGPUProjectionMatrix(camera.projectionMatrix, true) * worldToCameraMatrix
            
            UNITY_MATRIX_MVP   <=> Object To HClip Matrix <=> matrixP * matrixV * matrixM
            
            UNITY_MATRIX_T_MV  <=>                      
            UNITY_MATRIX_IT_MV <=>   
            */
            
            // v.vertex                                   => positionOS
            // mul(UNITY_MATRIX_M, v.vertex)              => positionWS
            // mul(UNITY_MATRIX_MV, v.vertex)             => positionVS
            // mul(UNITY_MATRIX_MVP, v.vertex)            => positionCS
            // float2(positionCS.x, -positionCS.y) / 

            /* 
            Various 变量

            float4 _Time : Time (t/20, t, t*2, t*3), use to animate things inside the shaders 
            时间： 用于Shasder中可动画的地方。
            
            float4 _SinTime : Sine of time: (t/8, t/4, t/2, t) 
            时间的正弦值。
            
            float4 _CosTime : Cosine of time: (t/8, t/4, t/2, t) 
            时间的余弦值
            
            float4 _ProjectionParams : 投影参数
            x is 1.0 or -1.0, negative if currently rendering with a flipped projection matrix 
            x为1.0 或者-1.0如果当前渲染使用的是一个反转的投影矩阵那么为负。 
            y is camera's near plane y是摄像机的*剪裁*面
            z is camera's far plane z是摄像机远剪裁*面
            w is 1/FarPlane. w是1/远剪裁*面
            
            float4 _ScreenParams : 屏幕参数
            x is current render target width in pixels x是当前渲染目标在像素值中宽度
            y is current render target height in pixels y是当前渲染目标在像素值中的高度
            z is 1.0 + 1.0/width z是1.0+1.0/宽度
            w is 1.0 + 1.0/height w是1.0+1.0/高度
            */

            v2f vert(appdata_base v)
            {
                v2f o;
                o.uv = v.texcoord.xy;
                o.positionCS = UnityObjectToClipPos(v.vertex);
                // ComputeScreenPos "UnityCG.cginc" 内置方法
                o.positionScreen01 = ComputeScreenPos(o.positionCS) / o.positionCS.w;
                return o;
            }
            
            half4 frag(v2f i) : COLOR
            {
                // 结果应该为 左下纯黑 右上纯黄
                return half4(i.positionScreen01.xy , 0, 1);
            }
            ENDCG
        }
    }
} 